package com.ganlebao.study.springcloudeurekaclient;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.netflix.discovery.shared.Application;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@SpringBootApplication
@RestController
public class SpringCloudEurekaClientApplication {

    @Autowired
    private EurekaClient eurekaClient;

    public static void main(String[] args) {
        SpringApplication.run(SpringCloudEurekaClientApplication.class, args);
    }

    @GetMapping("eureka-server")
    public Map<String, Object> allService() {
        Application application = this.eurekaClient.getApplication("eureka-client".toUpperCase());
        List<InstanceInfo> clients = application.getInstances();
        return clients.stream().collect(Collectors.toMap(InstanceInfo::getIPAddr, InstanceInfo::getPort));
    }

}
